package com.bosssoft.boss_exam_db.service.busines.impl;

import com.bosssoft.boss_exam_db.entity.Company;
import com.bosssoft.boss_exam_db.entity.Exam;
import com.bosssoft.boss_exam_db.entity.ExamExamPaper;
import com.bosssoft.boss_exam_db.entity.ExamPaper;
import com.bosssoft.boss_exam_db.enums.ExamPaperEnum;
import com.bosssoft.boss_exam_db.mapper.CompanyMapper;
import com.bosssoft.boss_exam_db.mapper.ExamMapper;
import com.bosssoft.boss_exam_db.model.vo.*;
import com.bosssoft.boss_exam_db.service.base.impl.BaseServiceImpl;
import com.bosssoft.boss_exam_db.service.busines.ExamExamPaperService;
import com.bosssoft.boss_exam_db.service.busines.ExamPaperService;
import com.bosssoft.boss_exam_db.service.busines.ExamService;
import com.bosssoft.boss_exam_db.util.MySqlUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class ExamServiceImpl extends BaseServiceImpl<Exam> implements ExamService {

    //注入ExamMapper
    @Autowired
    private ExamMapper examMapper;

    // 注入公司Mapper
    @Autowired
    private CompanyMapper companyMapper;

    // 注入试卷服务
    @Autowired
    private ExamPaperService examPaperService;

    //注入考试试卷服务
    @Autowired
    private ExamExamPaperService examExamPaperService;


    /***
     * @Author lys(cherish)
     * @Phone 17720799636
     * @Date  2019/3/26
     * @Description  根据当前用户所在公司、考试名称、查询考试相关信息 相关信息包括该考试是否选择了卷子，是否发布
     * @Param [company, examName, page, limit, sort, order]
     * @Return com.github.pagehelper.PageInfo<com.bosssoft.boss_exam_db.model.vo.ExamIsReleaseVO>
     * @Exception
     * @since JDK 1.8
     **/
    @Override
    public PageInfo<ExamIsReleaseVO> queryByCondition(Company company, String examName, Integer page, Integer limit, String sort, String order) {
        Example example = new Example(Exam.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("companyId", company.getId());
        if (!StringUtils.isEmpty(examName)) {
            criteria.andLike("examName", MySqlUtil.cloumLike(examName));
        }
        criteria.andEqualTo("deleted", false);

        PageHelper.startPage(page, limit).setOrderBy(sort + " " + order);
        List<Exam> exams = this.examMapper.selectByExample(example);
        List<ExamIsReleaseVO> examIsReleaseVOS = exams.stream().map(exam -> {
            List<ExamExamPaper> examExamPapers=examExamPaperService.queryExamPaperByExamId(exam.getId());

            ExamIsReleaseVO examIsReleaseVO = new ExamIsReleaseVO();
            BeanUtils.copyProperties(exam,examIsReleaseVO);
            if(examExamPapers!=null && examExamPapers.size()!=0){
                // 获取该考试对应的试卷
                ExamExamPaper examExamPaper = examExamPapers.get(0);
                examIsReleaseVO.setCheckTime(examExamPaper.getCheckTime());
                examIsReleaseVO.setAdminIdArr(examExamPaper.getAdminIdArr());
                examIsReleaseVO.setIsRelease(true);
            }else {
                examIsReleaseVO.setIsRelease(false);
            }
            return examIsReleaseVO;
        }).collect(Collectors.toList());
        PageInfo<ExamIsReleaseVO> examIsReleaseVOPageInfo =new PageInfo<>(examIsReleaseVOS);
        return examIsReleaseVOPageInfo;

//        PageHelper.startPage(page, limit).setOrderBy(sort + " " + order);
//        PageInfo<Exam> examPageInfo = new PageInfo<>(exams);
//        return examPageInfo;
    }

    /***
     * @Author lys(cherish)
     * @Phone 17720799636
     * @Date  2019/4/8
     * @Description 根据条件查询试卷
     * @Param [companyId, groupId, jobId, examPaperLevel, flag] 公司ID、集团ID、岗位ID、试卷难度、是否公开
     * @Return java.util.List<com.bosssoft.boss_exam_db.entity.ExamPaper>
     * @Exception
     * @since JDK 1.8
     **/
    @Override
    public List<ExamPaper> queryExamPaperByCondition(String companyId, String groupId, String jobId, Integer examPaperLevel, Integer flag) {
        Example example = new Example(ExamPaper.class);
        Example.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(jobId)) {
            criteria.andEqualTo("jobId", jobId);
        }
        if (!StringUtils.isEmpty(examPaperLevel)) {
            criteria.andEqualTo("examPaperLevel", examPaperLevel);
        }
        if (flag != null) {
            // 选择公司内部试卷
            if (flag.equals(ExamPaperEnum.COMPANY_PAPER.getCode())) {
                criteria.andEqualTo("companyId", companyId);
            } else if(flag.equals(ExamPaperEnum.GROUP_PAPER.getCode())) {
                // 选择集团开放试卷
                if (!StringUtils.isEmpty(groupId)) {
                    // 根据集团查询集团下的所有公司
                    Example companyExample = new Example(Company.class);
                    Example.Criteria companyExampleCriteria = companyExample.createCriteria();
                    companyExampleCriteria.andEqualTo("groupId",groupId);
                    companyExampleCriteria.andEqualTo("deleted",false);
                    List<Company> companyList = companyMapper.selectByExample(companyExample);
                    List<String> companyIds = companyList.stream().map(company -> company.getId()).collect(Collectors.toList());
                    criteria.andIn("companyId",companyIds);
                }
                criteria.andEqualTo("openFlag",ExamPaperEnum.GROUP_PAPER_OPEN.getCode());
            }
        }
        criteria.andEqualTo("deleted",false);
        List<ExamPaper> examPapers = examPaperService.selectByExample(example);
        return examPapers;
    }

    /***
     * @Author lys(cherish)
     * @Phone 17720799636
     * @Date  2019/3/31
     * @Description 查询公司先的所有试卷
     * @Param [companyId]
     * @Return java.util.List<com.bosssoft.boss_exam_db.entity.Exam>
     * @Exception
     * @since JDK 1.8
     **/
    @Override
    public List<Exam> queryExamByCompanyId(String companyId) {
        Example example = new Example(Exam.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtils.isEmpty(companyId)){
          return null;
        }
        criteria.andEqualTo("companyId",companyId);
        criteria.andEqualTo("deleted",false);
        List<Exam> exams = this.examMapper.selectByExample(example);
        return exams;
    }

    /***
     * @Author lys(cherish)
     * @Phone 17720799636
     * @Date  2019/4/8
     * @Description 根据考试ID查询考试-试卷信息
     * @Param [examId] 考试ID
     * @Return java.util.List<com.bosssoft.boss_exam_db.model.vo.ExamExamPaperVO>
     * @Exception
     * @since JDK 1.8
     **/
    @Override
    public List<ExamExamPaperVO> getExamAndPaperById(String examId) {
        return this.examMapper.getExamAndPaperById(examId);
    }

    /***
     * @Author lys(cherish)
     * @Phone 17720799636
     * @Date  2019/4/8
     * @Description 根据考试批次查询应聘者考试总分情况
     * @Param [examCode] 考试批次
     * @Return java.util.List<com.bosssoft.boss_exam_db.model.vo.ApplicantTotalScoreVO>
     * @Exception
     * @since JDK 1.8
     **/
    @Override
    public List<ApplicantTotalScoreVO> queryExamScoreByExamCod(String examCode) {
        List<ApplicantTotalScoreVO> applicantTotalScoreVOS = this.examMapper.queryExamScoreByExamId(examCode);
        return applicantTotalScoreVOS;
    }

    /***
     * @Author lys(cherish)
     * @Phone 17720799636
     * @Date  2019/4/2
     * @Description  根据考试ID、应聘人ID查询考试应聘者根据科目的考试情况
     * @Param [examId, appliantId]
     * @Return java.util.List<com.bosssoft.boss_exam_db.model.vo.QuestionSubjectScoreVO>
     * @Exception //TODO
     * @since JDK 1.8
     **/
    @Override
    public List<QuestionSubjectScoreVO> queryExamScoreBySubject(String examId, String appliantId) {
        List<QuestionSubjectScoreVO> questionSubjectScoreVOS = this.examMapper.queryQuestionSubjectScore(examId, appliantId);
        return questionSubjectScoreVOS;
    }

    /***
     * @Author lys(cherish)
     * @Phone 17720799636
     * @Date  2019/4/8
     * @Description 根据考试ID查询应聘者考试根据题型的分布情况
     * @Param [examId, appliantId] 考试ID、应聘者ID
     * @Return java.util.List<com.bosssoft.boss_exam_db.model.vo.QuestiontypeScoreVO>
     * @Exception
     * @since JDK 1.8
     **/
    @Override
    public List<QuestiontypeScoreVO> queryExamScoreByQuestionType(String examId, String appliantId) {
        List<QuestiontypeScoreVO> questiontypeScoreVOS = this.examMapper.queryQuestionTypeScore(examId, appliantId);
        return questiontypeScoreVOS;
    }

    /***
     * @Author lys(cherish)
     * @Phone 17720799636
     * @Date  2019/4/8
     * @Description 根据考试ID查询考试的科目分布情况
     * @Param [examId] 考试ID
     * @Return java.util.List<com.bosssoft.boss_exam_db.model.vo.SubjectTotalScoreVO>
     * @Exception
     * @since JDK 1.8
     **/
    @Override
    public List<SubjectTotalScoreVO> queryExamTotalScoreBySubject(String examId) {
        return this.examMapper.queryTotalScoreBySubject(examId);
    }

    /***
     * @Author lys(cherish)
     * @Phone 17720799636
     * @Date  2019/4/8
     * @Description 根据考试ID查看考试根据题型的分布情况
     * @Param [examId] 考试iD
     * @Return java.util.List<com.bosssoft.boss_exam_db.model.vo.QuestiontypeTotalScoreVO>
     * @Exception
     * @since JDK 1.8
     **/
    @Override
    public List<QuestiontypeTotalScoreVO> queryExamTotalScoreByQuestionType(String examId) {
        return this.examMapper.queryTotalScoreByQuestionType(examId);
    }

//    @Override
//    public List<ApplicantTotalScoreVO> queryExamScoreByExamId(String examId) {
//        List<ApplicantTotalScoreVO> applicantTotalScoreVOS = this.examMapper.queryExamScoreByExamId(examId);
//        return applicantTotalScoreVOS;
//    }


}
